package com.zyy.smartcity1129.me;

import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.JsonObject;
import com.zyy.smartcity1129.BaseFragment;
import com.zyy.smartcity1129.R;
import com.zyy.smartcity1129.data.BusMyOrderInfo;
import com.zyy.smartcity1129.data.MyOrdersInfo;
import com.zyy.smartcity1129.data.ResultInfo;
import com.zyy.smartcity1129.movie.MovieOrderFragment;
import com.zyy.smartcity1129.util.Http;
import com.zyy.smartcity1129.util.RvAd;
import com.zyy.smartcity1129.util.XCUtli;

import java.util.ArrayList;
import java.util.List;

import okhttp3.RequestBody;

/**
 * @ClassName: MyOrderFragment$
 * @Description: 作用
 * @Author: letquit
 * @CreateDate: 1/12/2021$
 * @Version: 1.0
 */
public class MyOrderFragment extends BaseFragment {
    private RecyclerView rvMyOrder;
    private List<MyOrdersInfo.RowsBean> orderList = new ArrayList<>();
    private List<BusMyOrderInfo.RowsBean> busOrderList = new ArrayList<>();
    RvAd<MyOrdersInfo.RowsBean> rvAd;
    RvAd<BusMyOrderInfo.RowsBean> rvAd1;
    private RecyclerView rvBusMyOrder;

    @Override
    protected int layId() {
        return R.layout.f_myorder;
    }

    @Override
    protected String title() {
        return "订单";
    }

    @Override
    protected boolean back() {
        return true;
    }

    @Override
    protected Boolean bottom() {
        return null;
    }

    @Override
    protected void init(View iv) {
        initView(iv);
        XCUtli xcUtli = new XCUtli(1);
        xcUtli.then(() -> {
            orderList = Http.getInstance().get("/prod-api/api/allorder/list", null, MyOrdersInfo.class, 1).getRows();
            xcUtli.addint();
        });
        xcUtli.then(() -> {
            busOrderList = Http.getInstance().get("/prod-api/api/bus/order/list", null, BusMyOrderInfo.class, 1).getRows();
            xcUtli.addint();
        });
        xcUtli.XCliveData.observe(getViewLifecycleOwner(), a -> {
            if (a.get() == 2) {
                initAdapter(orderList);
                initBusAdapter(busOrderList);
                rvMyOrder.setLayoutManager(new LinearLayoutManager(getContext()));
                rvBusMyOrder.setLayoutManager(new LinearLayoutManager(getContext()));
                rvMyOrder.setAdapter(rvAd);
                rvBusMyOrder.setAdapter(rvAd1);
            }
        });
    }

    private void initBusAdapter(List<BusMyOrderInfo.RowsBean> mBusOrderList) {
        rvAd1 = new RvAd<BusMyOrderInfo.RowsBean>(mBusOrderList, R.layout.rv_order) {
            private void initView(View vi) {
                tvRvOrder1 = (TextView) vi.findViewById(R.id.tvRvOrder1);
                tvRvOrder2 = (TextView) vi.findViewById(R.id.tvRvOrder2);
                tvRvOrder3 = (TextView) vi.findViewById(R.id.tvRvOrder3);
                tvRvOrder4 = (TextView) vi.findViewById(R.id.tvRvOrder4);
                tvRvOrder5 = (TextView) vi.findViewById(R.id.tvRvOrder5);
                tvRvOrder6 = (TextView) vi.findViewById(R.id.tvRvOrder6);
                tvRvOrder8 = (TextView) vi.findViewById(R.id.tvRvOrder8);
                tvRvOrder7 = (TextView) vi.findViewById(R.id.tvRvOrder7);
                tvRvOrder9 = (TextView) vi.findViewById(R.id.tvRvOrder9);
                tvRvOrder10 = (TextView) vi.findViewById(R.id.tvRvOrder10);
                tvRvOrder5.setVisibility(View.GONE);
                tvRvOrder8.setVisibility(View.GONE);
                tvRvOrder9.setVisibility(View.GONE);
            }

            private TextView tvRvOrder10;
            private TextView tvRvOrder9;
            private TextView tvRvOrder7;
            private TextView tvRvOrder8;
            private TextView tvRvOrder6;
            private TextView tvRvOrder5;
            private TextView tvRvOrder4;
            private TextView tvRvOrder3;
            private TextView tvRvOrder2;
            private TextView tvRvOrder1;

            @Override
            protected void initR(View vi, BusMyOrderInfo.RowsBean rowsBean, int p) {
                initView(vi);
                tvRvOrder1.setText(rowsBean.getPath());
                tvRvOrder2.setText("起点：" + rowsBean.getStart());
                tvRvOrder3.setText("终点：" + rowsBean.getEnd());
                tvRvOrder4.setText("订单编号: " + rowsBean.getOrderNum());
                tvRvOrder6.setText(rowsBean.getPrice() + "元");
                tvRvOrder10.setText("乘车日期：" + rowsBean.getCreateTime());
                if (String.valueOf(rowsBean.getStatus()).equals("0")) {
                    tvRvOrder7.setText("待支付");
                } else {
                    tvRvOrder7.setText("已支付");
                }
                tvRvOrder7.setOnClickListener(view -> {
                    if (rowsBean.getStatus() == 0) {
                        JsonObject jsonObject = new JsonObject();
                        jsonObject.addProperty("orderNum", rowsBean.getOrderNum());
                        jsonObject.addProperty("paymentType", "电子支付");
                        th(() -> {
                            String result = Http.getInstance().get("/prod-api/api/bus/pay", RequestBody.create(Http.m, jsonObject.toString()), ResultInfo.class, 2).getMsg();
                            busOrderList = Http.getInstance().get("/prod-api/api/bus/order/list", null, BusMyOrderInfo.class, 1).getRows();
                            ui(() -> {
                                Toast.makeText(getContext(), result, Toast.LENGTH_SHORT).show();
                                initBusAdapter(busOrderList);
                                rvBusMyOrder.setAdapter(rvAd1);
                            });
                        });
                    } else {
                        Toast.makeText(getContext(), "订单已支付", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        };
    }

    private void initAdapter(List<MyOrdersInfo.RowsBean> orderList) {
        rvAd = new RvAd<MyOrdersInfo.RowsBean>(orderList, R.layout.rv_list) {
            private void initView(View vi) {
                id = (TextView) vi.findViewById(R.id.id);
                orderTypeName = (TextView) vi.findViewById(R.id.orderTypeName);
                createTime = (TextView) vi.findViewById(R.id.createTime);
            }

            private TextView createTime;
            private TextView orderTypeName;
            private TextView id;

            @Override
            protected void initR(View vi, MyOrdersInfo.RowsBean rowsBean, int p) {
                initView(vi);
                createTime.setText(String.valueOf(rowsBean.getCreateTime()));
                orderTypeName.setText(rowsBean.getOrderTypeName());
                id.setText(String.valueOf(rowsBean.getId()));
                vi.setOnClickListener(view -> {
                    String orderType = rowsBean.getOrderType();
                    switch (orderType) {
                        case "movie":
                            add(new MovieOrderFragment());
                            break;
                    }
                });
            }
        };
    }

    private void initView(View iv) {
        rvMyOrder = (RecyclerView) iv.findViewById(R.id.rvMyOrder);
        rvBusMyOrder = (RecyclerView) iv.findViewById(R.id.rvBusMyOrder);
    }
}
